package cn.gson.crm.controller.position;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.PositionDao;
import cn.gson.crm.model.entity.Position;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/position/form")
public class PositionFormServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private PositionDao postDao = new PositionDao();
	
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String pId = request.getParameter("pid");
		System.out.println("pId:"+pId);
		if(pId != null){
			Position post = postDao.getPost(pId);
			System.out.println("post:"+post);
			request.setAttribute("post", post);
		}
		redirect("position?postStatus=1");
		//forward("position/position_form.jsp");
	}
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String postId = request.getParameter("postId");
		String postName = request.getParameter("postName");
		String postDescribe = request.getParameter("postDescribe");
		String postStatus = request.getParameter("postStatus");
		
		List<Position> position =  postDao.getPostByStatus(1);
		if(postName == null || postName.isEmpty()){
			errorForward("请输入职位名称", "position/position_form.jsp");
			return;
		}else if(position.get(0).getPostName().equals(postName)){
			errorForward("该职位已存在", "position/position_form.jsp");
			return;
		}
		
		Position post = new Position();
		if(postId != null && !postId.isEmpty()){
			post.setPostId(Integer.parseInt(postId));
		}
		post.setPostName(postName);
		post.setPostDescribe(postDescribe);
		post.setPostStatus(1);
		if(postDao.savePost(post)){
			redirect("position?postStatus="+postStatus);
			return;
		}
		//errorForward("请输入职位名称", "position/form");
	}

}
